﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "Coordinate3d.h"

namespace gcmp
{
    class ICurve2d;
    class IBody;
    class IPolygon;
    class IPolyCurve;

    struct CurveOffsetNode
    {
        CurveOffsetNode()
        {
            shouldErase = false;
            opCurve = nullptr;
        };

        bool shouldErase;   // 标记是否该别删除
        OwnerPtr<ICurve2d> opCurve;
        std::vector<OwnerPtr<ICurve2d>> noiseVector;  // 噪声曲线
    };

    struct CurveMergeNode
    {
        CurveMergeNode()
        {
        };

        //曲线是否应该加入当前节点
        bool ShouldAddCurve(const ICurve2d* pCur, double threshold) const;

        OwnerPtr<ICurve2d> m_opBaseLine;  // 基线，所有的线都往该条线上合并
        bool isLine;        // 基线是否是直线
        std::vector<OwnerPtr<ICurve2d>> curves;  // 需要merge的所有曲线
    };

    class GbmpStructureProfileSearchUtils
    {
    public:
        // 将Vector2d顺时针转90°
        static Vector2d Vector2dRoatate90ClockWise(const Vector2d& vec);

        // loop1是否在loop2内部，前提loop1和loop2是属于同一个polygon
        static bool IsLoop1InLoop2(const IPolyCurve* pLoop1, const IPolyCurve* pLoop2);

        // 一堆polygon做布尔并生成一个polygon
        static bool BoolOperateUnite(const std::vector<OwnerPtr<IPolygon>>& polygons, OwnerPtr<IPolygon>& result);
        
        // bodies分别投影然后做布尔并生成IPolygon
        static OwnerPtr<IPolygon> ProjectBodiesAndBoolUnite(const std::vector<const IBody*>& bodies, const Coordinate3d& coord);

        static double GetLoopArea(const IPolyCurve* pLoop);

        // 延长Curve1的end 和 curve2的start，然后求交（会改变pFirst和pSecond）
        // 最后一个参数为true，表明如果 min > max 自动交换，否则返回false
        static bool Curve1EndConnectCurve2Start(ICurve2d* pCurve1, ICurve2d* pCurve2, bool autoAdjustRangeMinMax);

        // 获取polygon中所有相离的外环，当面积小于minArea则舍掉
        static bool GetAllDisconnectedOutterLoop(const IPolygon* pPolygon, std::vector<OwnerPtr<IPolyCurve>>& result, double* pMinArea = nullptr);

        // 将curves分组，当相邻的曲线长度小于howLongIsNoise时分在一组,arcIsNotNoise圆弧是不是噪声
        static void PolygonToCurveGroup(std::vector<OwnerPtr<ICurve2d>>& curves, double howLongIsNoise, bool arcIsNotNoise,std::vector<OwnerPtr<CurveOffsetNode>> &groups);
    };
}
